<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta name="author" content="lzy">
    <title>lzy-</title>
    <style>
        ul,li{
            margin: 0;
            padding: 0;
            list-style: none;
        }
        #box{
            width: 520px;
            height: 280px;
            margin: 100px auto;
            position: relative;
        }
        #box .list li{
            position: absolute;
            left: 0;
            right: 0;
            display: none;
        }
        #box .list li.current{
            display: block;
        }
        #box .count{
            position: absolute;
            right: 10px;
            bottom: 10px;
        }
        #box .count li{
            float: left;
            width: 20px;
            height: 20px;
            background-color: #fa0;
            border-radius: 50%;
            text-align: center;
            line-height: 20px;
            color: white;
            margin-left: 10px;
            opacity: 0.8;
            cursor: pointer;
        }
        #box .count li.current{
            background-color: #f60;
            opacity: 1;
        }
    </style>
</head>
<body>
    <div id="box">
        <ul class="list">
            <li class="current"><img src="img/1.jpg" alt=""></li>
            <li><img src="img/2.jpg" alt=""></li>
            <li><img src="img/3.jpg" alt=""></li>
            <li><img src="img/4.jpg" alt=""></li>
            <li><img src="img/5.jpg" alt=""></li>
        </ul>
        <ul class="count">
            <li class="current">1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
            <li>5</li>
        </ul>
    </div>
</body>
</html>
<script>
    var box = document.querySelector('#box');
    var aLi = document.querySelectorAll('.list li');
    var aNum = document.querySelectorAll('.count li');
    // 记录当前显示的图片的索引
    var current=0;
    // 保存定时器
    var timer=null;

    // 先写自动的
    timer=setInterval(autoPlay,1000);
    function autoPlay(){
        current++;
        if(current>=aLi.length){
            current = 0;
        }
        show(current);
    }
    // 手动的
    for(var i=0;i<aNum.length;i++){
        aNum[i].index=i;
        aNum[i].onmouseover=function(){
            show(this.index);
        }
    }

    // 处理图片的显示和隐藏
    function show(index){
        // 手动改变是,需要更新current的值
        current = index;
        // 当前图片显示时,那么其他的图片应该隐藏
        for(var i=0;i<aLi.length;i++){
            aLi[i].style.display = 'none';
            aNum[i].className='';
        }
        // 让当前的图片显示
        aLi[index].style.display='block';
        // 改变图片对应数字的状态
        aNum[index].className='current';
    }

    // 当鼠标移入容器的时候,让自动播放停止,鼠标移出的时候,继续播放
    box.onmouseover = function(){
        clearInterval(timer);
    }
    box.onmouseout = function(){
        timer = setInterval(autoPlay,1000);
    }
</script>
